/*
 * @Author: 亚亚
 */
/*
 * @Author: 亚亚
 */
/*
 * @Author: 亚亚
 */
/*
 * @Author: 亚亚
 */
import { fileURLToPath, URL } from 'node:url'
import path from 'path'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import UnoCSS from 'unocss/vite'
import Pages from 'vite-plugin-pages'
import Layouts from 'vite-plugin-vue-layouts'
import Inspector from 'vite-plugin-vue-inspector'
import qiankun from 'vite-plugin-qiankun'
import viteCompression from 'vite-plugin-compression'
import { createStyleImportPlugin, VxeTableResolve } from 'vite-plugin-style-import'
import Inspect from 'vite-plugin-inspect'
import { visualizer } from 'rollup-plugin-visualizer';
// import Critters from 'critters';

//图标自动引入
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'

const pathSrc = path.resolve(__dirname, 'src')

export default defineConfig(({command:string, mode}) => {
  const env = loadEnv(mode, process.cwd())
  const isMicro = env.VITE_APP_IS_PA_MICRO === 'YES'
  const options = {
    // qiankun生产环境需要指定运行域名作为base
    base: isMicro ? env.VITE_APP_NAME : '/',
    plugins: [
      vue(),
      AutoImport({
        resolvers: [
          ElementPlusResolver(),
          // 自动导入图标组件
          IconsResolver({
            prefix: 'Icon',
          }),
        ],
        imports: [ //vue -> API自动引入 暂时注释，方便代码阅读
          'vue',
          'vue-router',
          'vue/macros',
          '@vueuse/core'
        ],
        dirs: [
          'src/composables',
          'src/stores',
        ],
        vueTemplate: true,
        dts: path.resolve(pathSrc, 'auto-imports.d.ts'),
      }),
      Components({  
        resolvers: [
          // 自动注册图标组件
          IconsResolver({
            enabledCollections: ['ep'],
          }),
          ElementPlusResolver(),
        ],
        //组件自动引入
        extensions: ['vue', 'md'],
        include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
        dts: path.resolve(pathSrc, 'components.d.ts'),
      }),
      Icons({
        autoInstall: true,
      }),
      UnoCSS(),
      Pages({
        dirs: [
          // 需要生成路由的文件目录
          {dir: 'src/views', baseRoute: `/${env.VITE_BASE_HASH_ROUTER}/${env.VITE_APP_NAME}`}
        ], 
        // dirs: 'src/views',
        exclude: ['**/components/*.vue']  // 排除在外的目录，即所有 components 目录下的 .vue 文件都不会生成路由
      }),
      Layouts({
        layoutsDirs: 'src/layout',  // 布局文件存放目录
        defaultLayout: 'index'  // 默认布局，对应 src/layout/index.vue
      }),
      createStyleImportPlugin({
        resolves: [
          VxeTableResolve()
        ],
      }),
      qiankun(env.VITE_APP_NAME),

      // visualizer(),
      // viteCompression()
      // Critters()

      Inspect()
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    server: {
      host: '0.0.0.0',
      open: !isMicro
    }
  }
  console.log(mode, env, '-------------');
  
  if(mode === 'production') {
    options.plugins.push(visualizer())
    options.plugins.push(viteCompression())
  }
  if(!isMicro) {
    options.plugins.push(
      Inspector({   //点击浏览器元素自动跳转到本地IDE，在乾坤里不起作用
        toggleButtonVisibility: 'always',
      })
    )
  }
  return options
})
